﻿using System;
using System.IO;
using Xunit;

namespace Adjunct.Build.Tasks
{
	public class UpdateNuspecDependenciesSpec
	{
		[Fact]
		public void NuspecDoesNotExist() {
			MockBuildEngine engine = new MockBuildEngine();
			UpdateNuspecDependencies task = new UpdateNuspecDependencies {
				NuspecFullPath = Path.Combine(TestHelper.GetProjectDirectory(), "DoesNotExist.nuspec"),
				ProjectFullPath = string.Empty,
				ExcludedReferences = string.Empty,
				BuildEngine = engine
			};
			task.Execute().ShouldBeFalse();
			TestHelper.Dump(engine.Log.ToString());
			//todo asserts
		}

		[Fact]
		public void UpdateGuardNuspec() {
			string sourcePath = Path.Combine(GetGuardRootPath(), "Source");
			MockBuildEngine engine = new MockBuildEngine();
			UpdateNuspecDependencies task = new UpdateNuspecDependencies {
				NuspecFullPath = Path.Combine(sourcePath, "Guard.nuspec"),
				ProjectFullPath = Path.Combine(sourcePath, "Guard.csproj"),
				ExcludedReferences = "system;system.core;xunit",
				BuildEngine = engine
			};
			task.Execute().ShouldBeTrue();
			TestHelper.Dump(engine.Log.ToString());
		}

		[Fact]
		public void UpdateGuardTestsNuspec() {
			string sourcePath = Path.Combine(GetGuardRootPath(), "Tests");
			MockBuildEngine engine = new MockBuildEngine();
			UpdateNuspecDependencies task = new UpdateNuspecDependencies {
				NuspecFullPath = Path.Combine(sourcePath, "Guard.Tests.nuspec"),
				ProjectFullPath = Path.Combine(sourcePath, "Guard.Tests.csproj"),
				ExcludedReferences = "system;system.core;xunit",
				BuildEngine = engine
			};
			task.Execute().ShouldBeTrue();
			TestHelper.Dump(engine.Log.ToString());
		}

		[Fact]
		public void UpdateStandalone() {
			string projectPath = TestHelper.GetProjectDirectory();
			MockBuildEngine engine = new MockBuildEngine();
			UpdateNuspecDependencies task = new UpdateNuspecDependencies {
				NuspecFullPath = Path.Combine(projectPath, "test.nuspec"),
				ProjectFullPath = string.Empty,
				ExcludedReferences = string.Empty,
				BuildEngine = engine
			};
			task.Execute().ShouldBeTrue();
			TestHelper.Dump(engine.Log.ToString());
		}

		[Fact]
		public void UpdateTestSpec() {
			string projectPath = TestHelper.GetProjectDirectory();
			MockBuildEngine engine = new MockBuildEngine();
			UpdateNuspecDependencies task = new UpdateNuspecDependencies {
				NuspecFullPath = Path.Combine(projectPath, "test.nuspec"),
				ProjectFullPath = Path.Combine(projectPath, "Adjunct.Build.Tests.csproj"),
				ExcludedReferences = "Adjunct.Build;Microsoft.Build.Framework;Microsoft.Build.Utilities.v4.0;System;System.Core;System.Xml;System.Xml.Linq;xunit",
				BuildEngine = engine
			};
			task.Execute().ShouldBeTrue();
			TestHelper.Dump(engine.Log.ToString());
		}

		private string GetGuardRootPath() {
			string path = TestHelper.GetProjectDirectory();
			path = Path.GetDirectoryName(path); // Tools
			path = Path.GetDirectoryName(path); // Adjunct
			path = Path.Combine(path, "Extensions", "Guard");
			return path;
		}
	}
}